package org.liaohailong.library.image.picasso

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.squareup.picasso.Picasso
import com.squareup.picasso.RequestCreator
import com.squareup.picasso.Target
import org.liaohailong.library.RootApplication
import org.liaohailong.library.image.ImageEngine
import org.liaohailong.library.image.ImageHelper
import java.lang.Exception

/**
 * Author: liaohailong
 * Date: 2019/3/8
 * Time: 10:51 AM
 * Description: Picasso引擎
 **/
class PicassoImageEngine : ImageEngine {
    private val picasso: Picasso = Picasso.Builder(RootApplication.INSTANCE).build()

    override fun setImage(imageView: ImageView,
                          url: String,
                          width: Int,
                          height: Int,
                          placeHolder: Drawable?,
                          errorDrawable: Drawable?,
                          callback: ImageEngine.OnImageEngineCallback?) {
        val requestCreator: RequestCreator = picasso
                .load(url)
                .resize(width, height)
                .onlyScaleDown()
        placeHolder?.let { requestCreator.placeholder(it) }
        errorDrawable?.let { requestCreator.error(errorDrawable) }
        requestCreator.into(object : Target {
            override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
                imageView.setImageDrawable(placeHolderDrawable)
            }

            override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
                imageView.setImageBitmap(bitmap)
                callback?.onImageAttachedComplete(imageView, url, true)
            }

            override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
                imageView.setImageDrawable(errorDrawable)
                callback?.onImageAttachedComplete(imageView, url, true)
            }
        })
    }

    override fun loadImage(context: Context,
                           url: String,
                           path: String,
                           width: Int,
                           height: Int,
                           callback: ImageEngine.OnImageEngineCallback) {
        picasso.load(url)
                .resize(width, height)
                .onlyScaleDown()
                .into(object : Target {
                    override fun onPrepareLoad(placeHolderDrawable: Drawable?) {

                    }

                    override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
                        ImageHelper.saveBitmapAsync(bitmap!!, path, object : ImageHelper.OnImageHelperCallback {
                            override fun onSavedComplete(success: Boolean, path: String) {
                                callback.onImageLoadedComplete(path, url, success)
                            }

                        })
                    }

                    override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
                        callback.onImageLoadedComplete(path, url, false)
                    }
                })
    }
}